import { Device, Page, ResultBean } from '@shared/bean';
import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { SimpleTableColumn, SimpleTableComponent } from '@delon/abc';
import { SFSchema } from '@delon/form';
import { DevicesListEditComponent } from './edit/edit.component';
import { DevicesListViewComponent } from './view/view.component';

@Component({
  selector: 'app-devices-list',
  templateUrl: './list.component.html',
})
export class DevicesListComponent implements OnInit {
  data: Page = new Page();
  devices: Device[] = [];
  searchSchema: SFSchema = {
    properties: {
      no: {
        type: 'string',
        title: '编号'
      }
    }
  };
  @ViewChild('st') st: SimpleTableComponent;
  columns: SimpleTableColumn[] = [
    { title: '编号', index: 'name' },
    { title: '产品类别', index: 'product.name' },
    { title: '头像', type: 'img', width: '150px', index: 'image' },
    { title: '时间', type: 'date', index: 'createTime' },
    {
      title: '',
      buttons: [
        { text: '查看', type: 'static', component: DevicesListViewComponent, click: 'reload' },
        { text: '编辑', type: 'static', component: DevicesListEditComponent, click: 'reload' },
      ]
    }
  ];

  constructor(private http: _HttpClient, private modal: ModalHelper) { }

  ngOnInit() {
    this.http.post('/api/devices/query').subscribe((res: ResultBean<Page>) => {
      this.data = res.data;
      this.devices = this.data.content;
    });
   }

  add() {
    this.modal
     .createStatic(DevicesListEditComponent, { i: { id: -1 } })
      .subscribe(() => this.st.reload());
  }

}
